package net.monkeybros.sbus;

import java.util.ArrayList;
import java.util.Random;
import java.util.Vector;

import net.monkeybros.sbus.model.InfoLinea;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class BusLines extends ListActivity {

	private static final String URL = "http://www.ayto-santander.es:9001";
	private ProgressDialog m_ProgressDialog = null;
	private Vector<InfoLinea> m_infoLinea = null;
	private InfoLineaAdapter m_adapter;
	private Runnable viewLineas;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bus_lines_list);
		m_infoLinea = new Vector<InfoLinea>();
		this.m_adapter = new InfoLineaAdapter(this, R.layout.bus_lines_list_row, m_infoLinea);
		setListAdapter(this.m_adapter);

		viewLineas = new Runnable() {
			@Override
			public void run() {
				getLineas();
			}
		};
		Thread thread = new Thread(null, viewLineas, "BusLines");
		thread.start();
		m_ProgressDialog = ProgressDialog.show(BusLines.this, "Please wait...", "Retrieving data ...", true);

	}

	private Runnable returnRes = new Runnable() {

		@Override
		public void run() {
			if (m_infoLinea != null && m_infoLinea.size() > 0) {
				m_adapter.notifyDataSetChanged();
				for (int i = 0; i < m_infoLinea.size(); i++)
					m_adapter.add(m_infoLinea.get(i));
			}
			m_ProgressDialog.dismiss();
			m_adapter.notifyDataSetChanged();
		}
	};

	private void getLineas() {
		try {

			Estructura estructura = new Estructura();
			estructura.globalBaseUrl = URL;

			m_infoLinea = estructura.GetLineasFilter(0);

			Log.i("ARRAY", "" + m_infoLinea.size());
		} catch (Exception e) {
			Log.e("BACKGROUND_PROC", e.getMessage());
		}
		runOnUiThread(returnRes);
	}

	private class InfoLineaAdapter extends ArrayAdapter<InfoLinea> {

		private int[] colors = new int[] { 0xFF550D2A, 0xFFFE000C, 0xFF9C1284, 0xFFF772AC, 0xFF0C2576, 0xFF0A55A3, 0xFFD05B00, 0xFFFF8700, 0xFFF0AE00, 0xFF005837, 0xFF019069, 0xFF59B124, 0xFFE2F23A, 0xFF0569A5, 0xFF87A1CC };

		private Vector<InfoLinea> items;

		private Random rnd;

		public InfoLineaAdapter(Context context, int textViewResourceId, Vector<InfoLinea> m_infoLinea) {
			super(context, textViewResourceId, m_infoLinea);
			this.items = m_infoLinea;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.bus_lines_list_row, null);
			}
			InfoLinea o = items.get(position);
			if (o != null) {
				TextView tlabel = (TextView) v.findViewById(R.id.BusLinesRowLabel);
				TextView bt = (TextView) v.findViewById(R.id.BusLinesRowName);
				if (tlabel != null) {
					rnd = new Random();
					tlabel.setText(o.getlabel());
					tlabel.setBackgroundColor(colors[position%colors.length]);
				}
				if (bt != null) {
					bt.setText(o.getnombre());
				}
			}
			return v;
		}
	}

}
